var app = getApp()
Page({
  data: {
    name: '',
    phone: '',
    wechatnum: '',
    cardType:'',
    showCardCode:false
  },
  onLoad: function (e) {
    var that = this;
    if(e&&e.type){
      this.setData({
        cardType:e.type
      })
    }
  },
  onShow: function () {
    var that = this;
    app.setNavtitle('门店会员');
    if (!app.globalData.plumSession) {
      app.wechatSq(that);
    } else {
      that.requestMemberInfo();
    }
  },
  requestMemberInfo: function () {
    var that = this;
    if (!that.data.memberInfo){
      wx.showLoading({
        title: '加载中',
      })
    }
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map:'applet_store_member_info'
      },
      success: function (res) {
        if (res.data.ec == 200) {
          that.setData({
            memberInfo:res.data.data
          })
          console.log(that.data.memberInfo);
        } else {
          console.log(res.data);
          app.errorTip(that, res.data.msg, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
        wx.stopPullDownRefresh();
      }
    });
  },
  onPullDownRefresh: function () {
    var that = this;
    that.requestMemberInfo();
  },
  // 会员卡介绍
  openCardIntroduce() {
    this.setData({
      showCardIntroduce: true
    })
  },
  closeCardIntroduce() {
    this.setData({
      showCardIntroduce: false
    })
  },
  tocardList: function (e) {// 开通会员
    wx.navigateTo({
      url: '/subpages/cardList/cardList'
    })
  },
  tomyProfile: function (e) {// 我的资料
    wx.navigateTo({
      url: '/pages/myProfile/myProfile'
    })
  },
  toCardCode: function (e) {//立即使用我的二维码
    wx.navigateTo({
      url: '/subpages/cardCode/cardCode'
    })
  },
  toCardConsumeRecord: function (e) {//消费记录
    var verify = e.currentTarget.dataset.verify;
    wx.navigateTo({
      url: '/subpages/cardConsumeRecord/cardConsumeRecord?verify=' + verify
    })
  },
  toStorelist: function (e) {//适用门店
    wx.navigateTo({
      url: '/subpages1/storeList/storeList'
    })
  },
  getPhoneNumber: function (e) {
    console.log(!app.globalData.memberInfo);
    console.log(e.detail.errMsg)
    console.log(e.detail.iv)
    console.log(e.detail.encryptedData)
    var that = this;
    wx.login({
      success: function (res) {
        if (res.code) {
          var data = {
            map: 'applet_three_save_phone',
            code: res.code,
            encryptedData: e.detail.encryptedData,
            iv: e.detail.iv
          };
          if (!data.encryptedData) {
            that.tomyProfile();
          } else {
            // that.requestMobile(data);
            that.tomyProfile();
          }
        } else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
    });
  },
  requestMobile: function (data) {
    var that = this;
    //发起请求，获取列表列表
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      mask: true,
      duration: 10000
    });
    console.log(app.globalData.requestUrl);
    console.log(data);
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          console.log(res.data.data);
          var mobile = res.data.data;
          var memberInfo = app.globalData.memberInfo;
          memberInfo.mobile = mobile;
          console.log(app.globalData.memberInfo);
          app.globalData.memberInfo = memberInfo;
          that.tomyProfile();
          // app.globalData.memberInfo = res.data.data.member;
        } else {
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideToast();
        wx.stopPullDownRefresh();
      }
    });
  },
})